package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * Created by Czl on 2015/7/25.
 */
@SuppressWarnings("serial")
public class Comment extends Model<Comment> {

	public static final Comment dao = new Comment();

	/**
	 * 根据学员ID-分页查询课程一级评论
	 * 
	 * @date 2016年9月7日11:08:14
	 * @param pageNumber
	 * @param pageSize
	 * @param courseId
	 * @return
	 */
	public Page<Comment> findCommentPageByStuId(int pageNumber, int pageSize, Long studentId) {
		return dao.paginate(pageNumber, pageSize, "select t.* ",
				" from t_common_comment t where t.target_type = 'course' and t.create_userid = ? order by created_time desc",
				studentId);
	}
	public Page<Comment> findAllCommentPageByStuId(int pageNumber, int pageSize, Long studentId) {
		return dao.paginate(pageNumber, pageSize, "select t.* ",
				" from t_common_comment t where t.create_userid = ? order by created_time desc",
				studentId);
	}

	/**
	 * 根据课程ID查询课程评论
	 * 
	 * @param courseId
	 * @return
	 */
	public List<Comment> findByCourseId(Long courseId) {
		List<Comment> commentList = dao.find("select * from t_common_comment where target_type = 'course' and target_id = ?",
				courseId);
		if (commentList != null) {
			return commentList;
		}
		return null;
	}

	/**
	 * 分页查询课程评论
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param courseId
	 * @return
	 */
	public Page<Comment> findCommentPage(int pageNumber, int pageSize, Long courseId) {
		return dao.paginate(pageNumber, pageSize, "select t.* ",
				" from t_common_comment t where t.target_type = 'course' and t.target_id = ? order by created_time desc",
				courseId);
	}

	public Page<Comment> findArticleCommentPage(int pageNumber, int pageSize, Long articleId) {
		return dao.paginate(pageNumber, pageSize, "select t.* ",
				" from t_common_comment t where t.target_type = 'article' and t.target_id = ? order by created_time desc",
				articleId);
	}

	/**
	 * 分页查询班级讨论
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param classId
	 * @return
	 */
	public Page<Comment> findPageByClassId(int pageNumber, int pageSize, Long classId) {
		return dao.paginate(pageNumber, pageSize, "select t.* ",
				" from t_common_comment t where t.target_type = 'clazz' and t.target_id = ? order by created_time asc", classId);

	}

	/**
	 * 保存评论
	 * 
	 * @param comment
	 * @param targetType
	 * @param targetId
	 * @param createUserId
	 * @param content
	 * @param studentName
	 */
	public void saveComment(Comment comment, String targetType, Long targetId, Long createUserId, String content,
			String studentName, String post, String headImg) {
		comment.set("target_type", targetType);
		comment.set("target_id", targetId);

		comment.set("create_userid", createUserId);
		comment.set("content", content);
		comment.set("created_time", new Date());
		comment.set("student_name", studentName);
		comment.set("student_post", post);
		comment.set("student_head_img", headImg);
		comment.remove("id").save();
	}

	/**
	 * 查询评论总条数
	 * 
	 * @param
	 * @return
	 */
	public Long getTotalComment(String targetType, Long targetId) {
		return Db.queryLong("select count(t.id) from t_common_comment t " + "where t.target_type = ? and t.target_id = ?",
				targetType, targetId);
	}

}
